Thread: [NEED HELP]warning: assignment makes integer from pointer without a cast

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    3

    [NEED HELP]warning: assignment makes integer from pointer without a cast

    I am trying to write code to find all the prime numbers before a user entered number. I started with a working program and when I tried to create a function, it got all messed up. Can you help fix it ?


    Code:
    #include <stdio.h>
    
    int is_prime( int num );
    int get_positive_integer(void);
    
    int main( ) {
      int upper;   /* upper limit to check    */
      int num;     /* current number to check */
      int isprime;
     /* used to flag if number is prime or not */
    
      upper = get_positive_integer();
    
      /* Outer Loop - loop over num in range [2, upper] */
    
      for (num = 2; num <= upper; num++) {
    
        /* Inner Loop - check if num is prime */
        is_prime(num); {
    
          isprime = 1;     /* 1 = TRUE; assume num is prime to start */
    
          isprime = is_prime;
        }
      if (isprime) {
          printf("%d is prime.\n", num);
        }
    
      }
    
      return 0;
    }
    
    
    int get_positive_integer(void)
    {
      int number;
    
      do {
        printf("Enter a positive integer: ");
        scanf("%d", &number);
      } while (number < 1);
    
      return number;
    }
    
    
    int is_prime( int num ){
      int div;     /* divisor                 */
    
      for (div=2; div<num; div++) {
    
        /* if div divides num evenly, then num % div == 0 */
    
      if (num % div == 0) {
          break;
    
        }
    
      }
      return 0;
      return 1;
    }
    Sorry about indentation mistakes in advance. It says the error is on line 23

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You are trying to set the integer isprime with the address of the function is_prime. I do NOT think you want to do that.

    Code:
    isprime = is_prime;
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    When you use the name of a function without any parentheses or other punctuation, you end up with a pointer to that function. You need to actually call the is_prime function.
    Code:
    isprime = is_prime(whatever number you want to check primality of);
    isprime and is_prime are too similar, you should rename one. This is not such a big deal in this little program, but as your programs get more complex, such things will cause you serious headaches, trying to keep track of which is the variable, which is the function, etc. Better yet, you don't even need the isprime variable, you can call the is_prime function directly inside the if condition on line 25.

    More notes:

    • Your "Inner Loop" on line 19 isn't actually a loop. It's just a single function call (which happens to have a loop in that function). Note also that the curly braces on lines 19 and 24 don't do anything.
    • 1 is a positive integer, but doesn't make sense as an upper limit. Unless the assignment specs say otherwise, you should restrict the upper limit to values >= 2.
    • div is the name of a function in the standard C library, I would change it to something like divisor.
    • The return 1; on line 62 never gets executed since it hits the return 0; first.
    • Your is_prime function is broken. It always returns 0 since the loop finishes whether it hits the break on line 56, or because div(isor) goes all the way up to num. Consider returning the correct value on line 56.


    There are also some improvements you can make to your is_prime function. You don't need to check divisors all the way up to num. Many people only check up to num/2, but you can do even better than that. Think carefully about the fact that factors pair up. For example, 2 is a factor of 24, it goes in 12 times. Likewise, 12 is a factor of 24, it goes in 2 times. Same with 3 and 8, and 4 and 6. Checking divisibility by 6, 8 and 12 is redundant. Think about where that inflection point is, where the factor pairs reverse and repeat.

    Also, checking even numbers is silly since they're all divisible by 2. Thus, you can save more time by checking only odd numbers in your loop, starting at 3. Just remember to check 2 explicitly before the loop.

  4. #4
    Registered User
    Join Date
    Apr 2014
    Posts
    3
    Thanks so much. I went back and changed a few things, but if I just take isprime out, as you say I should then how exactly would I make it call all the prime numbers ? This just kind of confused me haha

    EDIT: When I try to use is_prime in the if statement on line 25, I get an error so there's something wrong with that
    Last edited by sprockets; 04-09-2014 at 08:18 PM.

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    3
    When I run it now, I only get 2 as a prime number. Help!!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by laserlight View Post
    What is your current code?
    Agent 86. What's yours?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-24-2010, 04:41 PM
  2. Replies: 4
    Last Post: 03-03-2010, 01:06 PM
  3. Replies: 1
    Last Post: 08-11-2009, 12:09 PM
  4. Replies: 1
    Last Post: 12-23-2008, 09:39 AM
  5. Replies: 17
    Last Post: 02-13-2006, 01:19 PM